/*
 * ComplexNumber.cpp
 *
 *  Created on: Oct 29, 2012
 *  Author: Trenton Nall
 *  Assignment: Homework 7
 */

#include "ComplexNumber.h"

/**************************************************************
 Function ComplexNumber
 PURPOSE: constructs a new ComplexNumber
 PARAMETERS:
 	 real - real portion of the new #
 	 imaginary - imaginary portion of the new #
 CALLED BY: testQuestion3ComplexNumber
 SIDE EFFECTS: Sets the internal storage of for the real and imaginary
 	 components
 ***************************************************************/
ComplexNumber::ComplexNumber(double real, double imaginary) {
	this->real = real;
	this->imaginary = imaginary;
}

/**************************************************************
 Function add
 PURPOSE: adds the specified complex # to this complex #
 PARAMETERS:
 	 number - complex # to add to this number
 CALLED BY: testQuestion3ComplexNumber
 SIDE EFFECTS: Adds the real and imaginary components of specified # to this #.
 ***************************************************************/
void ComplexNumber::add(ComplexNumber &number) {
	// add the real and imginary components of the other #
	this->real = this->real + number.getReal();
	this->imaginary = this->imaginary + number.getImaginary();
}

/**************************************************************
 Function subtract
 PURPOSE: subtracts the specified complex # to this complex #
 PARAMETERS:
 	 number - complex # to subtract from this number
 CALLED BY: testQuestion3ComplexNumber
 SIDE EFFECTS: Adds the real and imaginary components of specified # to this #.
 ***************************************************************/
void ComplexNumber::subtract(ComplexNumber &number) {
	// subtracts the real and imaginary components of specified number
	// from this #
	this->real = this->real - number.getReal();
	this->imaginary = this->imaginary - number.getImaginary();
}

/**************************************************************
 Function multiplyBy
 PURPOSE: multiplies this complex # by the # specified
 PARAMETERS:
 	 number - complex # to multiply this number by
 CALLED BY: testQuestion3ComplexNumber
 SIDE EFFECTS: Multiplies the real and imaginary components of specified # to with #.
 ***************************************************************/
void ComplexNumber::multiplyBy(ComplexNumber &number) {
	// store original real value so that it is still available after its overwritten
	double originalReal = this->real;

	// calculate the multiplied value of the real component
	this->real = (this->real * number.getReal()) - (this->imaginary * number.getImaginary());

	// calculate the multiplied value of the imaginary components
	this->imaginary = (originalReal * number.getImaginary()) + (this->imaginary * number.getReal());
}

/**************************************************************
 Function divideBy
 PURPOSE: divides this # by the specified complex #
 PARAMETERS:
 	 number - complex # to divide this # by
 CALLED BY: testQuestion3ComplexNumber
 SIDE EFFECTS: Divides the real and imaginary components of this # by the specified #.
 ***************************************************************/
void ComplexNumber::divideBy(ComplexNumber &number) {

	// multiply top and bottom by the conjugate of the bottom
	ComplexNumber *top = new ComplexNumber(this->real, this->imaginary);
	ComplexNumber *bottom = new ComplexNumber(number.getReal(), number.getImaginary());
	ComplexNumber *conjugate = new ComplexNumber(bottom->getReal(), -bottom->getImaginary());
	top->multiplyBy(*conjugate);
	bottom->multiplyBy(*conjugate);

	// bottom will now only have a real component so divide the real and imaginary components of
	// the result by that remaining real constant
	this->real = top->getReal() / bottom->getReal();
	this->imaginary = top->getImaginary() / bottom->getReal();
}

/**************************************************************
 Function getReal
 PURPOSE: retrieves the real component of this complex #
 PARAMETERS: none
 CALLED BY: testQuestion3ComplexNumber
 SIDE EFFECTS: none
 ***************************************************************/
double ComplexNumber::getReal() const {
	return this->real;
}

/**************************************************************
 Function getImaginary
 PURPOSE: retrieves the imaginary component of this complex #
 PARAMETERS: none
 CALLED BY: testQuestion3ComplexNumber
 SIDE EFFECTS: none
 ***************************************************************/
double ComplexNumber::getImaginary() const {
	return this->imaginary;
}
